package servlet;

import pojo.Category;
import dao.CategoryDao;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/CategoryServlet")
@MultipartConfig
public class CategoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String m=request.getParameter("m");
        try {
            List<Category> categoryMsg = CategoryDao.getCategoryMsg();
            servletContext.setAttribute("categoryMsg",categoryMsg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(m.equals("queryCategory")){
            try {
                queryCategory(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //修改分类图书信息
        if(m.equals("updateCategory")){
            updateCategory(request,response);
        }
        else if(m.equals("addCategory")){
            addCategory(request,response);
        }


    }

    protected void addCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String c_id=request.getParameter("c_id");
        String c_name=request.getParameter("c_name");
        String c_descript=request.getParameter("c_descript");
        Part part=request.getPart("c_photo");
        String filename=part.getSubmittedFileName();
        if(filename.equals("")){
            filename="images/default.jpg";
        }else {
            filename="images/"+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            part.write(this.getServletContext().getRealPath("/")+filename);
        }
        Category category=new Category();
        category.setCname(c_name);
        category.setDesc(c_descript);
        category.setCid(c_id);
        category.setSampleImg(filename);
        if(CategoryDao.addCategory(category)==true){
            request.setAttribute("msg","添加图书分类信息成功");
            request.getRequestDispatcher("CategoryServlet?m=queryCategory").forward(request,response);
        }else {
            request.setAttribute("msg","添加图书分类信息失败");
            request.getRequestDispatcher("addCategory.jsp").forward(request,response);
        }
    }

    protected void updateCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String category_id=request.getParameter("category_id");
        String category_name=request.getParameter("category_name");
        String category_descript=request.getParameter("category_descript");
        Part part=request.getPart("book_photo");
        String filename=part.getSubmittedFileName();
        if(filename.equals("")){
            filename="images/default.jpg";
        }else {
            filename="images/"+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            part.write(this.getServletContext().getRealPath("/")+filename);
        }
        Category category=new Category();
        category.setCid(category_id);
        category.setCname(category_name);
        category.setDesc(category_descript);
        category.setSampleImg(filename);
        Boolean aBoolean = CategoryDao.updateCategory(category);
        if(aBoolean==true){
            request.setAttribute("msg","修改图书分类成功");
            request.getRequestDispatcher("CategoryServlet?m=queryCategory").forward(request,response);
        }else {
            request.setAttribute("msg","填写图书信息有误");
            request.getRequestDispatcher("CategoryServlet?m=queryCategory").forward(request,response);
        }
    }

    protected void queryCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<Category> categoryMsg = CategoryDao.getCategoryMsg();
        request.setAttribute("categoryMsg",categoryMsg);
        request.getRequestDispatcher("queryCategory.jsp").forward(request,response);
    }
}
